{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为________."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30 4]`，那么`data[2:100]`的值为________."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[30,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为________."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为________."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "97"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(    )`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`(    )`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`(    )`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．列表的`rindex()`方法返回指定元素在列表中最后一次出现的位置。`(    )`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入字符串3141592653587\n",
      "Counter({'5': 3, '3': 2, '1': 2, '4': 1, '9': 1, '2': 1, '6': 1, '8': 1, '7': 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "x = input(\"请输入字符串\")\n",
    "frequencies = Counter(x)\n",
    "print(frequencies)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串，输出其中只出现了一次的字符及其下标:31415926\n",
      "[('3', 0), ('4', 2), ('5', 4), ('9', 5), ('2', 6), ('6', 7)]\n"
     ]
    }
   ],
   "source": [
    "text = input('请输入一个字符串，输出其中只出现了一次的字符及其下标:')\n",
    "positions = [(ch,index) for index,ch in enumerate(text) if text.index(ch) == text.rindex(ch)]\n",
    "print(positions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．输入一个字符串,输出其中每个唯一字符最后一次出现的下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串:31415926\n",
      "0\n",
      "2\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "x = input('请输入一个字符串:')\n",
    "d = {}\n",
    "for i in x:\n",
    "     d[i] = d.get(i,0)+1\n",
    "for j in d:\n",
    "    if d[j] == 1:\n",
    "        print(x.index(j))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表:[1,4,56,9]\n",
      "61\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "lstA = eval(input('请输入一个列表:'))\n",
    "print(reduce(lambda x,y:x | y,lstA))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出`Yes`，否则输出`No`。要求使用切片实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个字符串31415926\n",
      "No\n"
     ]
    }
   ],
   "source": [
    "str1 = input('请输入一个字符串')\n",
    "str2 = str1[::-1]\n",
    "if str1==str2:\n",
    "    print('Yes')\n",
    "else:\n",
    "    print('No')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.range对象\n",
    "2.False\n",
    "3.None \n",
    "4.remove()\n",
    "5.[6,7,9,11,]\n",
    "6.{} 、键 、值 、键\n",
    "7.items()、keys()、values()\n",
    "8.dict(zip(a,b))\n",
    "9.b=a[::3]\n",
    "10.[5 fot i in range(10)]\n",
    "11.不可以"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "12."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "13."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def prime(x):\n",
    "    for i in range(2,x):\n",
    "        if x%i==0:\n",
    "            return False\n",
    "        if i==x-1:\n",
    "            return True\n",
    "output=filter(prime,range(2,1001))\n",
    "print(list(output))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "14.元素位置会改变"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "15."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[21, 24, 27, 38, 89, 10, 63, 13, 2, 17, 0, 32, 28, 93, 67, 95, 42, 79, 11, 69, 95, 90, 96, 49, 80, 66, 91, 62, 46, 88, 77, 59, 57, 25, 27, 57, 34, 7, 29, 6, 16, 20, 48, 17, 67, 47, 60, 44, 24, 83, 69, 8, 24, 42, 48, 2, 42, 12, 85, 80, 87, 65, 17, 23, 16, 59, 31, 14, 21, 67, 44, 78, 5, 29, 26, 53, 17, 85, 4, 13, 44, 5, 47, 34, 68, 34, 95, 22, 24, 26, 15, 71, 89, 96, 2, 33, 30, 14, 88, 66]\n",
      "21 : 2\n",
      "24 : 4\n",
      "27 : 2\n",
      "38 : 1\n",
      "89 : 2\n",
      "10 : 1\n",
      "63 : 1\n",
      "13 : 2\n",
      "2 : 3\n",
      "17 : 4\n",
      "0 : 1\n",
      "32 : 1\n",
      "28 : 1\n",
      "93 : 1\n",
      "67 : 3\n",
      "95 : 3\n",
      "42 : 3\n",
      "79 : 1\n",
      "11 : 1\n",
      "69 : 2\n",
      "95 : 3\n",
      "90 : 1\n",
      "96 : 2\n",
      "49 : 1\n",
      "80 : 2\n",
      "66 : 2\n",
      "91 : 1\n",
      "62 : 1\n",
      "46 : 1\n",
      "88 : 2\n",
      "77 : 1\n",
      "59 : 2\n",
      "57 : 2\n",
      "25 : 1\n",
      "27 : 2\n",
      "57 : 2\n",
      "34 : 3\n",
      "7 : 1\n",
      "29 : 2\n",
      "6 : 1\n",
      "16 : 2\n",
      "20 : 1\n",
      "48 : 2\n",
      "17 : 4\n",
      "67 : 3\n",
      "47 : 2\n",
      "60 : 1\n",
      "44 : 3\n",
      "24 : 4\n",
      "83 : 1\n",
      "69 : 2\n",
      "8 : 1\n",
      "24 : 4\n",
      "42 : 3\n",
      "48 : 2\n",
      "2 : 3\n",
      "42 : 3\n",
      "12 : 1\n",
      "85 : 2\n",
      "80 : 2\n",
      "87 : 1\n",
      "65 : 1\n",
      "17 : 4\n",
      "23 : 1\n",
      "16 : 2\n",
      "59 : 2\n",
      "31 : 1\n",
      "14 : 2\n",
      "21 : 2\n",
      "67 : 3\n",
      "44 : 3\n",
      "78 : 1\n",
      "5 : 2\n",
      "29 : 2\n",
      "26 : 2\n",
      "53 : 1\n",
      "17 : 4\n",
      "85 : 2\n",
      "4 : 1\n",
      "13 : 2\n",
      "44 : 3\n",
      "5 : 2\n",
      "47 : 2\n",
      "34 : 3\n",
      "68 : 1\n",
      "34 : 3\n",
      "95 : 3\n",
      "22 : 1\n",
      "24 : 4\n",
      "26 : 2\n",
      "15 : 1\n",
      "71 : 1\n",
      "89 : 2\n",
      "96 : 2\n",
      "2 : 3\n",
      "33 : 1\n",
      "30 : 1\n",
      "14 : 2\n",
      "88 : 2\n",
      "66 : 2\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "x = [random.randint(0,100)for i in range(100)]\n",
    "print(x)\n",
    "for i in x:\n",
    "    print(i,':',x.count(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个列表:[1,2,3,4,5,6]\n",
      "请输入第一个下标:2\n",
      "请输入第二个下标:5\n",
      "[3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "list = eval(input('请输入一个列表:'))\n",
    "a = eval(input('请输入第一个下标:'))\n",
    "b = eval(input('请输入第二个下标:'))\n",
    "if a>b:\n",
    "    for i in range(b,a):\n",
    "        print(list[b:a])\n",
    "        break\n",
    "elif a<b:\n",
    "    for i in range(a,b):\n",
    "        print(list[a:b])\n",
    "        break\n",
    "elif a==b:\n",
    "    print([])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "17."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "input the name:Liyuanhui\n",
      "您输入的键不存在!\n"
     ]
    }
   ],
   "source": [
    "d={'Tanghao':20,'Lihua':21}\n",
    "a=input('input the name:')\n",
    "print(d.get(a,'您输入的键不存在!'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "18."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[87, 75, 11, 84, 10, 66, 3, 66, 22, 68, 39, 55, 49, 52, 28, 76, 39, 92, 36, 2]\n",
      "[3, 10, 11, 22, 66, 66, 68, 75, 84, 87, 92, 76, 55, 52, 49, 39, 39, 36, 28, 2]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "x = [random.randint(0,100) for i in range(20)]\n",
    "print(x) \n",
    "x1=x[0:10]\n",
    "x1.sort()\n",
    "x[0:10]=x1\n",
    "x1=x[10:20]\n",
    "x1.sort(reverse=True)\n",
    "x[10:20]=x1\n",
    "print(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
